﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Shapes;
using System.Windows;

namespace ShapeDemo
{
    class ASPNETX_Adapter
    {
        #region 面
        /// <summary>
        /// 序列化WPF_Polygon数据
        /// </summary>
        /// <param name="lstPolygon"></param>
        /// <returns></returns>
        public static string SerializePolygon(List<ASPNETX_WPFPolygon> lstPolygon)
        {
            string result="";

            List<ASPNETX_Polygon> sd_polygon = new List<ASPNETX_Polygon>();

            foreach (ASPNETX_WPFPolygon item in lstPolygon)
            {
                ASPNETX_Polygon _polygon = new ASPNETX_Polygon();
                _polygon.CommonProperty = item.CommonProperty;
                _polygon.X = 0;
                _polygon.Y = 0;

                foreach (Point pitem in item.PolygonBody.Points)
                {
                    DPoint _p = new DPoint();
                    _p.X = pitem.X;
                    _p.Y = pitem.Y;

                    _polygon.PointsList.Add(_p);
                }

                sd_polygon.Add(_polygon);
            }

            result=SimpleSerializer.Serialize<List<ASPNETX_Polygon>>(sd_polygon);

            return result;
        }

        /// <summary>
        /// 反序列化WPF_Polygon数据
        /// </summary>
        /// <param name="strxml"></param>
        /// <returns></returns>
        public static List<ASPNETX_WPFPolygon> DeSerializePolygon(string strxml)
        {
            List<ASPNETX_WPFPolygon> result = new List<ASPNETX_WPFPolygon>();

            List<ASPNETX_Polygon> sd_polygon = new List<ASPNETX_Polygon>();
            sd_polygon = SimpleSerializer.Deserialize(typeof(List<ASPNETX_Polygon>),strxml) as List<ASPNETX_Polygon>;

            foreach (ASPNETX_Polygon item in sd_polygon)
            {
                ASPNETX_WPFPolygon _polygon = new ASPNETX_WPFPolygon();
                _polygon=ResourceCenter.NewPolygon();
                _polygon.PolygonBody.Points.Clear();
                
                _polygon.CommonProperty = item.CommonProperty;
                //_polygon.X = 0;
                //_polygon.Y = 0;

                foreach (DPoint pitem in item.PointsList)
                {
                    Point _p = new Point();
                    _p.X = pitem.X;
                    _p.Y = pitem.Y;

                    _polygon.PolygonBody.Points.Add(_p);
                }

                result.Add(_polygon);
            }

            return result;
        }
        #endregion

        #region 线
        /// <summary>
        /// 序列化WPF_Polyline数据
        /// </summary>
        /// <param name="lstPolygon"></param>
        /// <returns></returns>
        public static string SerializePolyline(List<ASPNETX_WPFPolyline> lstPolygon)
        {
            string result = "";

            List<ASPNETX_Polyline> sd_polyline = new List<ASPNETX_Polyline>();

            foreach (ASPNETX_WPFPolyline item in lstPolygon)
            {
                ASPNETX_Polyline _polyline = new ASPNETX_Polyline();
                _polyline.CommonProperty = item.CommonProperty;
                _polyline.X = 0;
                _polyline.Y = 0;

                foreach (Point pitem in item.PolylineBody.Points)
                {
                    DPoint _p = new DPoint();
                    _p.X = pitem.X;
                    _p.Y = pitem.Y;

                    _polyline.PointsList.Add(_p);
                }

                sd_polyline.Add(_polyline);
            }

            result = SimpleSerializer.Serialize<List<ASPNETX_Polyline>>(sd_polyline);

            return result;
        }

        /// <summary>
        /// 反序列化WPF_Polyline数据
        /// </summary>
        /// <param name="strxml"></param>
        /// <returns></returns>
        public static List<ASPNETX_WPFPolyline> DeSerializePolyline(string strxml)
        {
            List<ASPNETX_WPFPolyline> result = new List<ASPNETX_WPFPolyline>();

            List<ASPNETX_Polyline> sd_polyline = new List<ASPNETX_Polyline>();
            sd_polyline = SimpleSerializer.Deserialize(typeof(List<ASPNETX_Polyline>), strxml) as List<ASPNETX_Polyline>;

            foreach (ASPNETX_Polyline item in sd_polyline)
            {
                ASPNETX_WPFPolyline _polyline = new ASPNETX_WPFPolyline();
                _polyline = ResourceCenter.NewPolyline();
                _polyline.PolylineBody.Points.Clear();

                _polyline.CommonProperty = item.CommonProperty;
                //_polygon.X = 0;
                //_polygon.Y = 0;

                foreach (DPoint pitem in item.PointsList)
                {
                    Point _p = new Point();
                    _p.X = pitem.X;
                    _p.Y = pitem.Y;

                    _polyline.PolylineBody.Points.Add(_p);
                }

                result.Add(_polyline);
            }

            return result;
        }
        #endregion

        #region 点
        /// <summary>
        /// 序列化点
        /// </summary>
        /// <param name="lstPoint"></param>
        /// <returns></returns>
        public static string SerializePoint(List<ASPNETX_WPFPoint> lstPoint)
        {
            string result = "";

            List<ASPNETX_Point> sd_point = new List<ASPNETX_Point>();

            foreach (ASPNETX_WPFPoint item in lstPoint)
            {
                ASPNETX_Point _point = new ASPNETX_Point();
                _point.CommonProperty = item.CommonProperty;
                _point.X = item.PointBody.X;
                _point.Y = item.PointBody.Y;

                sd_point.Add(_point);
            }

            result = SimpleSerializer.Serialize<List<ASPNETX_Point>>(sd_point);

            return result;
        }

        /// <summary>
        /// 反序列化点
        /// </summary>
        /// <param name="strxml"></param>
        /// <returns></returns>
        public static List<ASPNETX_WPFPoint> DeSerializePoint(string strxml)
        {
            List<ASPNETX_WPFPoint> result = new List<ASPNETX_WPFPoint>();

            List<ASPNETX_Point> sd_point = new List<ASPNETX_Point>();
            sd_point = SimpleSerializer.Deserialize(typeof(List<ASPNETX_Point>), strxml) as List<ASPNETX_Point>;

            foreach (ASPNETX_Point item in sd_point)
            {
                ASPNETX_WPFPoint _point = new ASPNETX_WPFPoint();
                _point.CommonProperty = item.CommonProperty;
                Point p = new Point(item.X, item.Y);
                //_point.PointBody.X = item.X;
                //_point.PointBody.Y = item.Y;
                _point.PointBody = p;

                result.Add(_point);
            }

            return result;
        }
        #endregion

        #region 矩形
        /// <summary>
        /// 序列化矩形
        /// </summary>
        /// <param name="lstRect"></param>
        /// <returns></returns>
        public static string SerializeRect(List<ASPNETX_WPFRect> lstRect)
        {
            string result = "";

            List<ASPNETX_Rect> sd_rect = new List<ASPNETX_Rect>();

            foreach (ASPNETX_WPFRect item in lstRect)
            {
                ASPNETX_Rect _rect = new ASPNETX_Rect();
                _rect.CommonProperty = item.CommonProperty;
                _rect.X = item.Left;
                _rect.Y = item.Top;
                _rect.Width = item.Width;
                _rect.Height = item.Height;

                sd_rect.Add(_rect);
            }

            result = SimpleSerializer.Serialize<List<ASPNETX_Rect>>(sd_rect);

            return result;
        }

        /// <summary>
        /// 反序列化矩形
        /// </summary>
        /// <param name="strxml"></param>
        /// <returns></returns>
        public static List<ASPNETX_WPFRect> DeSerializeRect(string strxml)
        {
            List<ASPNETX_WPFRect> result = new List<ASPNETX_WPFRect>();

            List<ASPNETX_Rect> sd_rect = new List<ASPNETX_Rect>();
            sd_rect = SimpleSerializer.Deserialize(typeof(List<ASPNETX_Rect>), strxml) as List<ASPNETX_Rect>;

            foreach (ASPNETX_Rect item in sd_rect)
            {
                ASPNETX_WPFRect _rect = ResourceCenter.NewRect();

                _rect.CommonProperty = item.CommonProperty;

                //这里的算法还是需要改进一下
                
                _rect.Top = item.Y;
                _rect.Left = item.X;
                _rect.Height = item.Height;
                _rect.Width = item.Width;

                _rect.P1 = new Point(item.X, item.Y);
                _rect.P2 = new Point(item.X + item.Width, item.Y + item.Height);

                result.Add(_rect);
            }

            return result;
        }
        #endregion

        #region 圆
        /// <summary>
        /// 序列化圆
        /// </summary>
        /// <param name="lstEllipse"></param>
        /// <returns></returns>
        public static string SerializeEllipse(List<ASPNETX_WPFEllipse> lstEllipse)
        {
            string result = "";

            List<ASPNETX_Ellipse> sd_ellipse = new List<ASPNETX_Ellipse>();

            foreach (ASPNETX_WPFEllipse item in lstEllipse)
            {
                ASPNETX_Ellipse _ellipse = new ASPNETX_Ellipse();
                _ellipse.CommonProperty = item.CommonProperty;
                _ellipse.X = item.Left;
                _ellipse.Y = item.Top;
                _ellipse.Width = item.Width;
                _ellipse.Height = item.Height;

                sd_ellipse.Add(_ellipse);
            }

            result = SimpleSerializer.Serialize<List<ASPNETX_Ellipse>>(sd_ellipse);

            return result;
        }

        /// <summary>
        /// 反序列化圆
        /// </summary>
        /// <param name="strxml"></param>
        /// <returns></returns>
        public static List<ASPNETX_WPFEllipse> DeSerializeEllipse(string strxml)
        {
            List<ASPNETX_WPFEllipse> result = new List<ASPNETX_WPFEllipse>();

            List<ASPNETX_Ellipse> sd_ellipse = new List<ASPNETX_Ellipse>();
            sd_ellipse = SimpleSerializer.Deserialize(typeof(List<ASPNETX_Ellipse>), strxml) as List<ASPNETX_Ellipse>;

            foreach (ASPNETX_Ellipse item in sd_ellipse)
            {
                ASPNETX_WPFEllipse _ellipse = ResourceCenter.NewEllipse();
                
                _ellipse.CommonProperty = item.CommonProperty;

                _ellipse.Top = item.Y;
                _ellipse.Left = item.X;
                _ellipse.Height = item.Height;
                _ellipse.Width = item.Width;

                _ellipse.P1 = new Point(item.X, item.Y);
                _ellipse.P2 = new Point(item.X + item.Width, item.Y + item.Height);

                result.Add(_ellipse);
            }

            return result;
        }
        #endregion
    }
}
